package com.example.service.impl;

import com.example.dto.PostsDTO;
import com.example.mapper.PostsMapper;
import com.example.result.CommonResult;
import com.example.service.PostsService;
import com.example.vo.PostsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject："smart-property"
 * @BelongsPackage："com.example.service.impl"
 * @CreateTime："2022-11-14 09:29"
 * @Description：帖子实现类
 * @Author：LK
 */
@Service
public class PostsServiceImpl implements PostsService {

    @Autowired
    private PostsMapper postsMapper;

    /**
     * @description:帖子全查、分页
     * @author: lk
     * @date: 2022/11/14 9:56
     * @return: java.util.List<com.example.dto.PostsDTO>
     **/
    @Override
    public CommonResult postsCheck(PostsVO postsVO) {
        Map<String, Object> map = new HashMap<>();
        try{
            // 计算每页数据的开始
            postsVO.setPageNum((postsVO.getPageNum() - 1) * postsVO.getPageSize());
            // 帖子全查数据总数
            Integer total = postsMapper.postsCheckTotal(postsVO);
            // 帖子全查数据
            List<PostsDTO> postsDTOS = postsMapper.postsCheck(postsVO);
            if(postsDTOS != null){
                map.put("msg",postsDTOS);
                map.put("total",total);
                return CommonResult.ok(map);
            }
        }catch (Exception e){
            return CommonResult.fail();
        }
        return CommonResult.fail();
    }

    /**
     * @description:删除帖子
     * @author: lk
     * @date: 2022/11/14 13:45
     * @param: postsNumber
     * @return: java.lang.Integer
     **/
    @Override
    public Integer deletePosts(String postsNumber) {
        return postsMapper.deletePosts(postsNumber);
    }
}
